//
//  RCUIPhotoTools.m
//  xappSmartParkiOS
//
//  Created by kabu on 2018/6/14.
//  Copyright © 2018年 Alibaba. All rights reserved.
//

#import "RCUIPhotoTools.h"
#import "ZFActionSheet.h"
#import<AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import<AssetsLibrary/AssetsLibrary.h>
#import "CameraAlertViewController.h"
@interface RCUIPhotoTools ()<ZFActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    RCUIPhotoToolsBlock callBlock;
}
@property (nonatomic, strong) UIImagePickerController *imgPicker;
@property (nonatomic, weak) UIViewController *currentVc;
@end
@implementation RCUIPhotoTools

+(instancetype)showCuurentVc:(UIViewController*)cuurentVc canEidit:(BOOL)canEdit callBack:(RCUIPhotoToolsBlock)callBack{
    RCUIPhotoTools *photoT=[RCUIPhotoTools new];
    photoT.canEditImg=canEdit;
    [photoT showAlertCurrentVc:cuurentVc callBack:callBack];
    return photoT;
}


- (void)showAlertCurrentVc:(UIViewController*)currentVc callBack:(RCUIPhotoToolsBlock)callBack{
    callBlock=callBack;
    _currentVc=currentVc;
    ZFActionSheet *sheet = [ZFActionSheet actionSheetWithTitle:nil confirms:@[@"拍照",@"相册"] cancel:@"取消" style:ZFActionSheetStyleDefault];
    sheet.delegate = self;
    [sheet show];
//    self.sheet=sheet;
}

/**
 展示相机选择器
 
 @param cuurentVc 当前vc
 @param canEdit 能否编辑图片
 @param c_delegate 代理
 @return 实例
 */
+(instancetype)showCuurentVc:(UIViewController*)cuurentVc canEidit:(BOOL)canEdit delegate:(id<RCUIPhotoToolsDelegate>)c_delegate{
    RCUIPhotoTools *photoT=[RCUIPhotoTools new];
    photoT.canEditImg=canEdit;
    [photoT showAlertCurrentVc:cuurentVc delegate:c_delegate];
    return photoT;
}


/**
 展示相机选择器
 
 @param currentVc 当前vc
 @param c_delegate 代理
 */
- (void)showAlertCurrentVc:(UIViewController*)currentVc delegate:(id<RCUIPhotoToolsDelegate>)c_delegate{
    self.delegate =c_delegate;
    _currentVc=currentVc;
    ZFActionSheet *sheet = [ZFActionSheet actionSheetWithTitle:nil confirms:@[@"拍照",@"相册"] cancel:@"取消" style:ZFActionSheetStyleDefault];
    sheet.delegate = self;
    [sheet show];
//    self.sheet=sheet;
}

/**
 懒加载照片选择器

 @return 照片选择器
 */
-(UIImagePickerController*)imagePicker{
    if (_imgPicker==nil) {
        _imgPicker=[[UIImagePickerController alloc]init];
        _imgPicker.modalPresentationStyle= UIModalPresentationOverFullScreen;
        _imgPicker.delegate=self;
        _imgPicker.allowsEditing=self.canEditImg;
    }
    return _imgPicker;
}
- (UIViewController*)rootVc{
    UIViewController *rot=[UIApplication sharedApplication].keyWindow.rootViewController;
    return rot;
}

/**
 获取当前Vc

 @return vc
 */
- (UIViewController*)currentVc{
    if (_currentVc) {
        return _currentVc;
    }
    return [self rootVc];
}

/**
 打开相机

 @param pvc viewcontroller
 */
-(void)showCameraVc:(UIViewController*)pvc{
    
    [self imagePicker].sourceType=UIImagePickerControllerSourceTypeCamera;
    [self.currentVc presentViewController:[self imagePicker] animated:YES completion:^{
        //
    }];
}

/**
 打开相册

 @param pvc viewcontroller
 */
-(void)showAlbumVc:(UIViewController*)pvc{
    [self imagePicker].sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self.currentVc  presentViewController:[self imagePicker] animated:YES completion:^{
        //
    }];
}
/**
 获得图片后传递给代理
 
 @param image 从图片库或者相机获得的图片
 */
- (void)photoGetImg:(UIImage*)image info:(NSDictionary*)info{
    if (callBlock) {
        callBlock(image,info);
    }
    if ([self.delegate respondsToSelector:@selector(image:info:)]) {
        [self.delegate image:image info:info];
    }
}
- (BOOL)authCameraControl{
    
    //相机权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。可能是家长控制权限
        authStatus ==AVAuthorizationStatusDenied)  //用户已经明确否认了这一照片数据的应用程序访问
    {
        //        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您未打开相机权限，去开启吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        //        alert.tag=100;
        //        [alert show];
        __weak typeof(self) wself=self;
        [CameraAlertViewController showAuthCameraAlert:self.currentVc callBack:NULL];
        return NO;
    }
    return YES;
    
}
- (BOOL)authLibControl{
    //相册权限
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    if (author ==ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied){
        //        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您未打开相册权限，去开启吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        //        alert.tag=101;
        //        [alert show];
        [CameraAlertViewController showAuthCameraAlert:self.currentVc callBack:NULL];
        return NO;
    }
    return YES;
}
#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image;
    if (self.canEditImg) {
        image =[info objectForKey:UIImagePickerControllerEditedImage];
    }else{
        image =[info objectForKey:UIImagePickerControllerOriginalImage];
    }
    __weak typeof(self) weakSelf = self;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        [weakSelf photoGetImg:image info:info];
    }];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    __weak typeof(self)wSelf=self;
    [self.imgPicker dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark ZFActionSheetDelegate
- (void)clickAction:(ZFActionSheet *)actionSheet atIndex:(NSUInteger)index{
    if (index==0) {
        [self showCameraVc:self.currentVc];
    }else if(index==1){
        [self showAlbumVc:self.currentVc];
    }
}
@end
